clear
set more off

cap log close
log using "$logdir/explain_all.log", replace

use $fromsasdir/result if wg_amt/num_emp > 20 & num_emp > 0

destring naics3, replace force
qui replace naics3 = 0 if missing(naics3)

destring naics6, replace force
qui replace naics6 = 0 if missing(naics6)



*********************************
*	R^2 addition		*
*********************************

foreach var in ertc ertc_amendment {


	gen any_`var' = `var' > 0

	qui reghdfe any_`var', a(tp)
	local b1_`var' = e(r2_a)

	qui reghdfe any_`var', a(bin#tp)
	local b2_`var' = e(r2_a)

	qui reghdfe any_`var', a(bin#tp#naics3)
	local b3_`var' = e(r2_a)


	qui reghdfe any_`var', a(bin#tp#naics6)
	local b4_`var' = e(r2_a)

	qui reghdfe any_`var', a(bin#tp#naics6#prep_id)
	local b5_`var' = e(r2_a)

	di "Done `var'"
}


foreach var in ertc ertc_amendment {
	clear
	qui set obs 5
	gen spec = _n
	gen r2_a = .

	forval i = 1/5 {
		cap replace r2_a = `b`i'_`var'' if spec == `i'
	}

	qui save $outdir/explain_`var', replace
	di "`var'"
    list
}




cap log close



